<?php
	class financeMod extends commonMod{
		public function __construct(){
			parent::__construct();
		}
		
		//添加应收应付款首页
		public function index(){
			$this->display();
			
		}
		//财务管理
		public function insertOutput(){
			if(!empty($_GET['rollPage'])){
				$rollPage  = intval($_GET['rollPage']);			
			}else{
				$rollPage  = 1;
			}
			$totalRows = $this->model->table('staff_detail')->count();
			$startFrom  = ($rollPage-1)*30;
			$page = parent::page('index', $totalRows, 30, $rollPage,2);
			$resultInfo = $this->model->table('money')->limit("$startFrom,30")->select();
			//echo $this->model->getSql();
			//dump($result);
			if($resultInfo){
				foreach ( $resultInfo as $key=>$result ){
					//dump($result);
					$resultInfo[$key]['insertTime'] = date('Y-m-d',$result['insertTime']);
					if(!empty($result['updateTime'])){
						$resultInfo[$key]['updateTime'] = date('Y-m-d',$result['updateTime']);
					}					
					$resultInfo[$key]['shouldTime'] = date('Y-m-d',$result['shouldTime']);
					switch ($result['statusInfo']){
						case 0;
							$resultInfo[$key]['statusInfo'] = '应收';
						break;
						case 1;
							$resultInfo[$key]['statusInfo'] = '应付';
						break;
						case 2;
							$resultInfo[$key]['statusInfo'] = '已收';
						break;
						case 3;
							$resultInfo[$key]['statusInfo'] = '已付';
						break;
					}
				}
				$this->assign('page', $page);
				$this->assign('count', $totalRows);
				$this->assign('resultInfo', $resultInfo);
				$this->display();
			}else{
				$this->error('数据查询失败，请返回重试！如仍有问题请联系网络部！！');
			}
		}
		
		//添加应收应付款
		public function addInOut(){
			//收支类型定义(tag为数据库字段)：'tag'=0=>支出，'tag'=1=>收入。
			//收支状态(statusInfo为数据库字段)：'statusInfo'=0=>应收，'statusInfo'=1=>应付，'statusInfo'=2=>已收，'statusInfo'=3=>已付
			if(!empty($_POST['yes'])){
				$data['tag']         = intval($_POST['tag']);
				$data['unitName']    = in($_POST['unitName']);
				$data['sumInfo']     = in($_POST['sumInfo']);
				$data['shouldTime']  = exchangeDate($_POST['shouldTime']);
				$data['explainInfo'] = in($_POST['explainInfo']);
				$data['remarks']     = in($_POST['remarks']);
				$data['statusInfo']  = intval($_POST['statusInfo']);
				$data['insertTime']  = time();
			}else{
				$this->error('关键参数丢失！！');
			}
			$result = $this->model->table('money')->data($data)->insert();
			if($result){
				$this->success('添加成功！！','insertOutput');
			}else{
				$this->error('添加失败！！');
			}
		}
		//修改收支项目
		public function changFinance(){
			if(!empty($_GET['id'])){
				$id = intval($_GET['id']);
			}else{
				$this->error('关键参数丢失！');
			}
			$result = $this->model->table('money')->where('`id`='.$id)->find();
			$result['shouldTime'] = date('Y-m-d',$result['shouldTime']);
			$this->assign('result', $result);
			$this->display();
		}
		
		//修改收支项目
		public function update(){
			if(!empty($_POST['yes'])){
				$data['id']          = intval($_POST['id']);
				$data['tag']         = intval($_POST['tag']);
				$data['unitName']    = in($_POST['unitName']);
				$data['sumInfo']     = in($_POST['sumInfo']);
				$data['shouldTime']  = exchangeDate($_POST['shouldTime']);
				$data['explainInfo'] = in($_POST['explainInfo']);
				$data['remarks']     = in($_POST['remarks']);
				$data['statusInfo']  = intval($_POST['statusInfo']);
				$data['updateTime']  = time();
			}else{
				$this->error('关键参数丢失！！');
			}
			$result = $this->model->table('money')->data($data)->where('`id`='.$data['id'])->update();
			if($result){
				$this->success('修改成功！！','insertOutput');
			}else{
				$this->error('修改失败！！');
			}
		}
		
		//删除收支项目
		public function delF(){
			if(!empty($_POST['id'])){
				$id = intval($_POST['id']);
			}else{
				$this->error('参数丢失，请刷新后重试，如仍未能解决，请联系网络部！！');
			}
			$result = $this->model->table('money')->where('`id`='.$id)->delete();
			if($result){
				$this->success('删除成功');
			}else{
				$this->error('删除失败！');
			}
		}
		
		//生成历史记录，生成后不能修改
		public function changeF(){
		if(!empty($_POST['id'])){
				$id = intval($_POST['id']);
				$data['history '] = 1;
			}else{
				$this->error('参数丢失，请刷新后重试，如仍未能解决，请联系网络部！！');
			}
			$result = $this->model->table('money')->data($data)->where('`id`='.$id)->update();
			if($result){
				echo $msg = '修改成功,此记录将不能被任何人改动';
			}else{
				echo $msg = '修改成功';
			}
		}
		
	}
	
	
?>